#include <string.h>
#include <iostream>
using std::cout;
using std::endl;
//C风格字符串
void test(){
    //字符数组
    //多预留一位用来填充末尾\0
    //下面的两种写法都是可以的，其中第二种较为常用一些
    char str[6] = {'h','e','l','l','o'};
    char str2[6] = "hello";
    cout << str2 << endl;
    //能够变更指向，能够修改字符串的值
    str2[0] = 'H';
    cout << str2 << endl;
    //str2 = "world";error不可以实现
}

void test2(){
    //字符指针:可以指向两个位置(堆区，常量区)
    const char * pstr = "hello";
    pstr = "world";
    //pstr[0] = 'H';error
    char * pstr2 = new char[6]();
    strcpy(pstr2, "hello");
    cout << pstr << endl;
    cout << pstr2 << endl;
    delete [] pstr2;
}

void test3(){
    char * pstr = new char[6]();
    strcpy(pstr, "hello");
    cout << pstr << endl;
    //可以修改字符串的值
    pstr[0] = 'H';
    cout << pstr << endl;

    delete [] pstr;
    //变更指向
    pstr = nullptr;
    pstr = new char[6]();
    strcpy(pstr, "world");

    delete [] pstr;
    pstr = nullptr;
}

//应该使用strlen求解字符串的长度
void test4(){
    //求解字符串的长度
    char str[6] = "hello";
    const char * pstr = "world";
    cout << sizeof(str) << endl;
    cout << sizeof(pstr) << endl;
    cout << strlen(str) << endl;
    cout << strlen(pstr) << endl;
}

//字符串的拼接
void test5(){
    char str1[6] = "hello";
    char str2[6] = "world";
    int length = strlen(str1) + strlen(str2) + 1;
    char str3[length];
    strcpy(str3, str1);
    strcat(str3, str2);
    cout << str3 << endl;
}

int main()
{
    test5();
    return 0;
}

